package com.easy.common.utils;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.easy.common.utils.ip.IpOrAddress;
import com.easy.common.utils.uuid.SnowFlake;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 元对象处理程序 工具
 *
 * @author RuoYu
 * @date 2022/11/20
 */
@Component
@Slf4j
public class MetaObjectHandlerUtils implements MetaObjectHandler {

    /**
     * 插入填充
     *
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", LocalDateTime.now());//平台用户
        metaObject.setValue("createAddress", IpOrAddress.getIp() + IpOrAddress.getAddress());//平台用户

        metaObject.setValue("loginTime", LocalDateTime.now());//平台用户
        metaObject.setValue("loginLocation", IpOrAddress.getIp() + IpOrAddress.getAddress());//平台用户

    }

    /**
     * 跟新操作，自动填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("loginTime", LocalDateTime.now());//平台用户
        metaObject.setValue("loginLocation", IpOrAddress.getIp() + IpOrAddress.getAddress());//平台用户
    }
}
